﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Security.Cryptography;
using System.IO;
using InnovTech.Util.Crypto;

namespace InnovTech.Util.Test.Crypto
{
    [TestClass]
    public class TestStreamCrypto
    {

        static PasswordDeriveBytes pdb = new PasswordDeriveBytes("halow",
           new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 
            0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});

        byte[] key = pdb.GetBytes(32);
        byte[] iv = pdb.GetBytes(16);

        [TestMethod]
        public void TestMethod1()
        {
            Rijndael alg = Rijndael.Create();
            alg.Key = key;
            alg.IV = iv;

            string testStr = "afdasjfpwfädsf#asfdsfaj";


            //MemoryStream encrypted = CryptoUtil.Encrypt(testStr, alg.CreateEncryptor());

            //string decrypted = CryptoUtil.Decrypt(encrypted, alg.CreateDecryptor()) as string;

            RijndaelCryptor cryptor = new RijndaelCryptor();
            MemoryStream encrypted = cryptor.Encrypt(testStr);

            string decrypted = cryptor.Decrypt(encrypted) as string;


            Assert.AreEqual(testStr, decrypted);
        }

        public class RijndaelCryptor : StreamCryptor
        {
            public override SymmetricAlgorithm Algo
            {
                get
                {
                    PasswordDeriveBytes pdb = new PasswordDeriveBytes("halow",
                                new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 
                                            0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});

                    byte[] key = pdb.GetBytes(32);
                    byte[] iv = pdb.GetBytes(16);

                    Rijndael alg = Rijndael.Create();
                    alg.Key = key;
                    alg.IV = iv;
                    return alg;
                }
            }
        }

    }
}
